package com.example.operation.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.operation.system.entity.UserInfo;
import com.example.operation.system.service.UserInfoService;
import com.example.substation.common.FrameResponse;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * @author liqiang
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    private final UserInfoService userInfoService;

    public AuthController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    /**
     * 登陆
     * @param userInfo
     * @return
     */
    @PostMapping("login")
    public FrameResponse login (@ModelAttribute UserInfo userInfo, HttpServletRequest request) {
       return FrameResponse.success(userInfoService.login(userInfo, request));
    }

    /**
     * 插入用户
     * @param userInfo
     * @return
     */
    @PostMapping("")
    public FrameResponse insert (@ModelAttribute UserInfo userInfo) {
        return FrameResponse.success(userInfoService.insert(userInfo));
    }

    /**
     * 修改用户
     * @param userInfo
     * @return
     */
    @PutMapping("")
    public FrameResponse update (@ModelAttribute UserInfo userInfo) {
        return FrameResponse.success(userInfoService.update(userInfo));
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public FrameResponse get(@PathVariable("id") Integer id) {
        return FrameResponse.success(userInfoService.getById(id));
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public FrameResponse delete(@PathVariable("id") Integer id) {
        return FrameResponse.success(userInfoService.removeById(id));
    }

    /**
     * 分页查询
     * @param userInfo
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("paging")
    public FrameResponse paging (@ModelAttribute UserInfo userInfo, @RequestParam int page, @RequestParam int limit) {
        return FrameResponse.success(userInfoService.paging(userInfo, page, limit));
    }

    /**
     * 根据对象查询数据
     * @param userInfo
     * @return
     */
    @GetMapping("")
    public FrameResponse list (@ModelAttribute UserInfo userInfo) {
        return FrameResponse.success(userInfoService.list(new QueryWrapper<>(userInfo)));
    }
}
